package com.sopaths.utility.text;

import java.security.MessageDigest;

import org.apache.log4j.Logger;

public class Encryptor {

	private static Logger logger = Logger.getLogger(Encryptor.class);

	public static String SHA_256(String text) {
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
			messageDigest.update(text.getBytes());
			byte[] bytes = messageDigest.digest();
			String result = convertToHexString(bytes);
			return result;
		} catch (Exception ex) {
			logger.error("An occurred while digesting text by SHA-256.", ex);
			throw new RuntimeException(ex);
		}
	}

	private static String convertToHexString(byte[] bytes) {
		StringBuilder builder = new StringBuilder();
		for (byte b : bytes) {
			//(b & 0xff) will convert byte to unsigned integer
			builder.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
		}
		return builder.toString();
	}

	public static void main(String[] args) {
		String text = "Hello Kevin1";
		System.out.println(Encryptor.SHA_256(text));
	}
}
